package com.easymin.weixin.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.easymin.weixin.common.api.DaijiaApi;
import com.easymin.weixin.controller.v5.utils.ErrorMapV5;
import com.easymin.weixin.controller.v5.utils.JsonUtil;
import com.easymin.weixin.controller.v5.utils.ResultV5;
import com.easymin.weixin.entity.WxApplication;
import com.easymin.weixin.manager.WxApplicationManager;
import com.easymin.weixin.service.NewsService;

/**
 * 广告位业务类
 * @author zane
 *
 */
@Service
public class DefaultNewsService implements NewsService {

	@Autowired
	private DaijiaApi daijiaApi;
	  
	@Autowired
	private WxApplicationManager wxApplicationManager;


	@Override
	public ResultV5 getNewsListByServiceType (Long companyId, String serviceType, String appKey) {

		WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
		// 开始调用
		String server = wxApplication.getAddress();
		String rs = daijiaApi.getWeixinV5News(server, companyId, serviceType);

		// 开始解析
		ResultV5 result = null;
		try {
			Map<String, ?> resultMap = JsonUtil.Parse2Map(rs);
			result = ErrorMapV5.ERR_SUCCESS.clone();
			result.setData(resultMap);
		} catch (Exception e) {
			e.printStackTrace();
			result = ErrorMapV5.ERR_SYSTEM;
		}
		return result;
	}
}
